// 获取weather.html对象函数
// function obtain() {
//     var date = document.getElementsByClassName("sky");
//     var weather = document.getElementsByClassName("wea");
//     var temperature = document.getElementsByClassName("tem");
//     var windDirection = document.getElementsByClassName("win");
//     console.log(date);
//     console.log(weather);
//     console.log(temperature);
//     console.log(windDirection[0].getElementsByTagName("span")[0].textContent);
// }

// 获取后端数据
function queryWeather(cityName, callback) {
    var xhr; //定义一个变量,用于存放XMLHttpRequest对象
    // var cityName = document.getElementsByClassName("search-input")[0].value;
    if (window.XMLHttpRequest) {
        //创建 Ajax 对象
        xhr = new XMLHttpRequest();
    } else if (window.ActiveXObject) {  // IE浏览器的创建方式
        //创建 Ajax 对象
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
    }
    var url = "http://127.0.0.1:8000/weather/q?cityName=" + cityName
    // get方式,配置 Ajax 对象
    xhr.open("get", url, true);

    //接收响应
    xhr.onreadystatechange = function () {  // 设置回调函数
        // 判断异步对象的状态
        if (xhr.readyState == 4) {
            // 判断交互是否成功
            if (xhr.status == 200) {
                // 获取服务器响应的数据
                var res = xhr.responseText;
                // 解析数据,json.parse将转res换成js对象
                res = JSON.parse(res);
                // console.log(res);
                callback(res); // 调用回调函数，并传递响应结果
            }

        }

    }

    xhr.send();
}

// $(function(){
//     dealWithData();
// });

// 处理后端数据，将数据填充到weather.html指定位置
function dealWithData() {
    // 获取html对象
    var date = document.getElementsByClassName("date");
    var weather = document.getElementsByClassName("wea");
    var temperature = document.getElementsByClassName("tem");
    var windDirection = document.getElementsByClassName("win");
    // 测试
    // console.log(date);
    // console.log(weather);
    // console.log(temperature);
    // console.log(windDirection);

    var url1 = window.location.href; // 获取网页url
    var cityName = url1.split("=")[1]; // 根据“=”分割
    cityName = decodeURI(cityName); // 解码成中文
    var position = document.getElementById("position")
    position.textContent = cityName;

    queryWeather(cityName, function (res) {
        var res1 = res;
        if (res1.code == "200") {
            // console.log(res1);
            var data = res1.data;
            // console.log(data);
            for (var i = 0; i < 7; i = i + 1) {
                var dataPerDay = data[i];
                date[i].textContent = dataPerDay.日期;
                weather[i].textContent = dataPerDay.天气;
                temperature[i].getElementsByTagName("span")[0].textContent = dataPerDay.最高温度;
                temperature[i].getElementsByTagName("i")[0].textContent = dataPerDay.最低温度;
                windDirection[i].getElementsByTagName("span")[0].textContent = dataPerDay.风向;
                windDirection[i].getElementsByTagName("i")[0].textContent = dataPerDay.风速;
            }
        } else {
            alert("天气信息查询失败！");
        }
    });
}
